import tool from "@/utils/tool";
import auth from "@/api/model/auth";
import system from "@/api/model/system";
import { testMenu } from "../testMenu";
import { ElAlert, ElMessageBox } from "element-plus";
import { options,getNowTime } from "@/utils/request/requestCache";
const user = {
	state: {
		token: tool.cookie.get("TOKEN"),
		name: "",
		avatar: "",
		roles: [],
		permissions: [],
		userInfo: {},
	},

	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token;
		},
		SET_NAME: (state, name) => {
			state.name = name;
		},
		SET_USERINFO: (state, userInfo) => {
			state.userInfo = userInfo;
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar;
		},
		SET_ROLES: (state, roles) => {
			state.roles = roles;
		},
		SET_PERMISSIONS: (state, permissions) => {
			state.permissions = permissions;
		},
	},

	actions: {
		// 登录
		Login({ commit }, userInfo) {
			const username = userInfo.username.trim();
			const password = userInfo.password;
			const code = userInfo.code;
			const token = userInfo.token;
			const REMEMBER_ME = tool.cookie.get("REMEMBER_ME");
			return new Promise((resolve, reject) => {
				auth.login
					.post({ username, password, code, token })
					.then((res) => {
						let { token: resToken } = res.data;
						tool.cookie.set("TOKEN", resToken, {
							expires: REMEMBER_ME ? 24 * 60 * 60 : 0,
						});
						commit("SET_TOKEN", resToken);
						resolve(res);
					})
					.catch((error) => {
						reject(error);
					});
			});
		},

		// 获取用户信息
		GetInfo({ commit }) {
			return new Promise((resolve, reject) => {
				auth.getInfo
					.get()
					.then((res) => {
						const { userInfo: user, permissions, menus } = res.data;
						// if (res.roles && res.roles.length > 0) {
						// 	// 验证返回的roles是否是一个非空数组
						// 	commit("SET_ROLES", res.roles);
						// 	commit("SET_PERMISSIONS", res.permissions);
						// 	tool.data.set("SET_PERMISSIONS", res.permissions);
						// 	tool.data.set("SET_ROLES", res.roles);
						// } else {
						// 	commit("SET_ROLES", ["ROLE_DEFAULT"]);
						// }
						// tool.data.set("MENU", user.menus);
						if (menus.length == 0) {
							this.islogin = false;
							ElMessageBox.confirm(
								"当前用户无任何菜单权限，请联系系统管理员",
								"无权限访问",
								{
									type: "error",
									closeOnClickModal: false,
									center: true,
									beforeClose: (action, instance, done) => {
										done();
									},
								}
							)
								.then(() => {})
								.catch(() => {});
							reject();
							return;
						}
						// tool.data.set("MENU", menus);
						tool.data.set("MENU", testMenu);
						user.id = user.userId;
						user.name = user.userName;
						tool.data.set("USER_INFO", user);
						tool.data.set("PERMISSIONS", permissions);
						commit("SET_USERINFO", user);
						resolve(res);
					})
					.catch((error) => {
						reject(error);
					});
			});
		},
		// 向后端请求路由数据
		GenerateRoutes() {
			return new Promise((resolve, reject) => {
				system.menu.myMenus
					.get()
					.then((res) => {
						// 区分登陆角色
						// var menu = null;
						// if (this.form.username == "admin") {
						//     menu = await this.$API.system.menu.myMenus.get();
						// } else {
						//     menu = await this.$API.demo.menu.get();
						// }
						if (res.data.length == 0) {
							this.islogin = false;
							ElAlert(
								"当前用户无任何菜单权限，请联系系统管理员",
								"无权限访问",
								{
									type: "error",
									center: true,
								}
							);
							return false;
						}
						// tool.data.set("MENU", testMenu);
						resolve(res);
					})
					.catch((error) => {
						reject(error);
					});
			});
		},

		// // 退出系统
		LogOut({ commit }) {
			return new Promise((resolve, reject) => {
				auth.loginout
					.get()
					.then(() => {
						tool.cookie.remove("TOKEN");
						tool.data.remove("MENU");
						tool.data.remove("PERMISSIONS");
						tool.data.remove("DASHBOARDGRID");
						tool.data.remove("grid");
						tool.data.remove("SEARCH_HISTORY");
						tool.data.remove("DEFAULTEMAIL");
						tool.data.remove("SELECTEMAIL");
						tool.data.remove("SELECTEMAILUSERID");
						tool.data.remove("SET_USERINFO");
						tool.data.remove("USER_INFO");
						tool.data.remove("USER_INFO");
						tool.cookie.remove('TOKEN')
						tool.session.clear();
						window.sessionStorage.clear();
						window.sessionStorage.setItem(options.storageKey, JSON.stringify({ data: {}, storageExpire: getNowTime() }));
						commit("SET_TOKEN", "");
						commit("SET_USERINFO", null);
						commit("SET_NAME", "");
						commit("SET_AVATAR", "");
						commit("SET_ROLES", null);
						commit("SET_PERMISSIONS", null);
						commit("clearViewTags")
						commit("clearKeepLive")
						commit("clearIframeList")
						resolve();
					})
					.catch((error) => {
						reject(error);
					});
			});
		},

		// // 前端 登出
		// FedLogOut({ commit }) {
		// 	return new Promise((resolve) => {
		// 		commit("SET_TOKEN", "");
		// 		removeToken();
		// 		resolve();
		// 	});
		// },
	},
};

export default user;
